<?php
/********************
 *@param function:基本設置 model
 *@param datetime 2011-6-7
 *@param author solgh
 ********************/
class Settings_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_settings';

  function __construct(){
    parent::__construct();
    log_message('debug','Settings_mdl Model class Initialized');
  }

  /****
   *查詢所settings,加分页
   *$data :查询参数.
   */
  public function get_settings($data=array()){
    extract($data);
    $this->db->where('status',1);
    isset($group) && !empty($group)?$this->db->where('group',$group):'';
    isset($key) && !empty($key)?$this->db->where('key',$key):'';
    isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
    return $this->db->get(self::TAB_NAME);
    
  }
  
  /****
   *查詢所settings core MY_controller使用
   *$data :查询参数.
   */
  public function get_core_settings($data=array()){
    extract($data);
    $this->db->where('status',1);
    isset($group) && !empty($group)?$this->db->where('group',$group):'';
    isset($no_group) && !empty($no_group)?$this->db->where('group !=',$no_group):'';
    isset($key) && !empty($key)?$this->db->where('key',$key):'';
    $this->db->cache_off();
    $query=$this->db->get(self::TAB_NAME);
    $this->db->cache_on();
    return $query;
    
  }
  /*****
   * 根據key來刪除
   */
  public function delete_settings($settings_key=''){
    if(!empty($settings_key)){
      $this->db->where("key",$settings_key);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_settings_by_where($where=array()){
    if(!empty($where)){
      $data=array();
      extract($where);
      $this->db->where('status',1);
      isset($group) && !empty($group)?$this->db->where('group',$group):'';
      isset($key) && !empty($key)?$this->db->where('key',$key):'';
      $get_result=$this->db->get(self::TAB_NAME);
      if($get_result->num_rows()>0){
        $data=$get_result->row_array();
      }
      $get_result->free_result();
      return $data;
    }
  }
  /****
   * 統計所有的settings
   */
  public function totle_settings($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->where('status',1);
      isset($group) && !empty($group)?$this->db->where('group',$group):'';
      isset($key) && !empty($key)?$this->db->where('key',$key):'';
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /***
   * 修改一個settings
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_settings($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 settings
   */
  public function add_settings($data=array()){
    if(!empty($data)){
      $this->db->insert(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 清空 settings
   */
  public function empty_table(){
    $this->db->empty_table(self::TAB_NAME);
  }
  
  /***
   * 根據key來修改
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_settings_by_key($data=array(),$key=''){
    if(!empty($data) && !empty($key)){
      $this->db->where('key',$key);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /***
   * 根據key group來修改
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_settings_by_keyGroup($data=array()){
    if(!empty($data) && is_array($data)){
      extract($data);
      isset($group) && !empty($group)?$this->db->where('group',$group):'';
      isset($key) && !empty($key)?$this->db->where('key',$key):'';
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
}
